package cn.com.xiaoshoujia.utils;

import cn.com.xiaoshoujia.activities.exception.ActivityException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by yangrui on 2017/7/14.
 */
public class DateUtils {

    static final String format = "yyyy-MM-dd HH:mm:ss";

    static DateFormat dateTimeFormat = new SimpleDateFormat(format);

    /**
     * @Author: yangrui
     * @Description: 获取时间戳
     * @Date: 下午5:48 2017/7/17
     */
    public static String getTimeStrap() {
        return System.currentTimeMillis() + "";
    }

    /**
     * @Author: yangrui
     * @Description: 获取系统当前时间str
     * @Date: 上午10:16 2017/7/14
     */
    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(new Date());
    }

    /**
     * @Author: yangrui
     * @Description: 日期字符串转时间戳
     * @Date: 下午3:13 2017/7/14
     */
    public static String date2TimeStamp(String date_str) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            Date date = sdf.parse(date_str);
            return String.valueOf(date.getTime());
        } catch (Exception e) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Date date = null;
            try {
                date = sdf.parse(date_str);
            } catch (ParseException e1) {
                throw new ActivityException("日期格式有误");
            }
            return String.valueOf(date.getTime());
        }
    }

    /**
     * @Author: dulei
     * @Description: 日期格式化yyyy-MM-dd HH:mm:ss
     * @Date 下午7:13 2017/7/22
     */
    public static String getDateTimeFormat(Date date) {
        return dateTimeFormat.format(date);
    }
}
